//
//  YTFVistor.swift
//  sinaWeibo
//
//  Created by pro on 16/2/25.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit
import SnapKit

//创建代理协议
protocol YTFVisitorDelegate: NSObjectProtocol{
    //设置代理方法
    func didRegister()
    
    func didLogin()
}
class YTFVisitor: UIView {
    
    //设置代理属性
    var delegate: YTFVisitorDelegate?
    
    //自定义视图的步骤
    //第一步：重写构造方法
    override init(frame: CGRect) {
        //调用父类的方法
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //调用私有方法，进行视图的设置和约束
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二步：创建一个私有方法，添加视图和设置约束
    private func setupUI(){
        
        //添加控件
        addSubview(homeIconImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        //旋转动画居中对齐
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //居中图片约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
        
        //提示约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(216)
        }
        
        //注册按钮约束
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        //登录按钮约束
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        //添加遮罩的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    //添加动画
    func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_2_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5
        
        //切换视图的时候，保证动画不要被移除
        animation.removedOnCompletion = false
        
        //添加
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    //对外提供一个方法，修改所需要的值
    func setInfo(message: String,image: String?,animation: Bool){
        
        if let img = image {
            
            iconImageView.image = UIImage(named: img)
            homeIconImageView.hidden = animation
        
        }else{
            homeIconImageView.hidden = !animation
        }
        
        messageLabel.text = message
        
    }
    
    
    //第三部：懒加载
    //首页动画图片
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        
    }()
    
    //通用图片
    private lazy var iconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
    }()
    
    //提示语
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        //设置label的属性
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        return label
        
    }()
    
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
    //遮罩视图
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
        
    }()
    
    
    func  register(){
//        print("注册")
        //代理调用方法
        delegate?.didRegister()
    }
    
    func login(){
//        print("登录")
        //代理调用方法
        delegate?.didLogin()
    }
    
}















